<template>
  <view>
    <slot></slot>
  </view>
</template>

<script lang="ts" setup>
import {computed, provide, reactive} from "vue";

interface CuFormProps {}
interface SetFieldParam {
  name: string
  value: any
}
withDefaults(defineProps<CuFormProps>(), {

})
const fields = reactive<Record<string, any>>({})
const validate = reactive<Record<string, boolean>>({})
provide('fields',fields)
provide('validate', validate)

defineExpose({
  submit: function (): Promise<any>{
    return new Promise((resolve, reject) => {
      for (let key in validate){
        if (!validate[key]){
          reject(`required item ${key}`)
        }
      }

      resolve(fields)
    })
  },

  setFields: function (arr: SetFieldParam[]): void {
    arr.forEach(p => {
      fields[p.name] = p.value
    })
  }
})
</script>

<style scoped>

</style>
